class Solution
{
public:
    vector<long long> mostFrequentIDs(vector<int> &nums, vector<int> &freq)
    {
        int n = nums.size();
        unordered_map<int, long long> count;

        auto compare = [](pair<int, long long> &a, pair<int, long long> &b)
        {
            return a.second < b.second;
        };
        priority_queue<pair<int, long long>, vector<pair<int, long long>>, decltype(compare)> pq(compare);

        vector<long long> result;
        for (int i = 0; i < n; ++i)
        {
            count[nums[i]] += freq[i];
            pq.push({nums[i], count[nums[i]]});
            while (!pq.empty() && count[pq.top().first] != pq.top().second)
            {
                pq.pop();
            }
            if (!pq.empty())
            {
                result.push_back(pq.top().second);
            }
            else
            {
                result.push_back(0);
            }
        }
        return result;
    }
};